package com.example.mvvm;

import android.app.ProgressDialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.mvvm.databinding.ActivityLoginBinding;

/**
 * Created by xzh on 2018/3/3.
 * 邮箱 ：347649749@qq.com
 * 版本 ： ver 1.0
 * 功能 ：MVVM 数据绑定（已经支持双向绑定）
 */

public class LoginActivity extends AppCompatActivity {

    private UserModel userModel;
    private ActivityLoginBinding binding ;
    private ProgressDialog dialog;
    private OnButtonClickListener mClick;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        mClick = new OnButtonClickListener();
        dialog = new ProgressDialog(this);

        binding.setOnLoginClick(mClick);
    }

    private class OnButtonClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.login:

                    //如果在控件中设置android:text="@{user.username}" 或 android:text="@{user.password}"
                    //然后设置一下内容
                    userModel = new UserModel("zhangsan","789456");
                    userModel.pass.set("2577");
                    userModel.name.set("205253");
                    userModel.newName.set("didadida");
                    binding.setUser(userModel);
                    //点击之后，该控件马上显示你设置的username 和 password
                    doLogin();
                    break;
                default:
                    break;
            }
        }
    }

    private void doLogin() {
        //获取文本框的内容进行校验
        int code = userModel.checkUserValidity(binding.userName.getText().toString(),binding.userPassword.getText().toString());
        //校验结果返回
        if (code == 1){
            Toast.makeText(this,"login success",Toast.LENGTH_LONG).show();
        }else if (code == -2){
            Toast.makeText(this,"用户名或密码为空",Toast.LENGTH_LONG).show();
        }else if(code == -1){
            /*binding.userName.setError("username is error or password is error");
            binding.userPassword.setError("username is error or password is error");*/
            //设置文本编辑框的内容
            //全局改变
            Toast.makeText(this,"用户名或密码错误",Toast.LENGTH_LONG).show();
            userModel = new UserModel("username error","password error");
            binding.setUser(userModel);
            userModel.pass.set("159");
            userModel.name.set("3578");

            /*
            *
            * */
            /*binding.userName.setText("");
            binding.userPassword.setText("");
            binding.userName.setHint("username   error");
            binding.userPassword.setHint("password   error");
            userModel.setUsername(binding.userName.getText().toString());
            userModel.setPassword(binding.userPassword.getText().toString());*/
        }else if (code == -3){
            Toast.makeText(this,"UserModel is null",Toast.LENGTH_LONG).show();
        }
    }
}
